#!/usr/bin/env Rscript

rm(list = ls())
options(stringsAsFactors=FALSE)

suppressMessages(library(dplyr))

setwd("")

cat("\nReading..\n")
## INDIV <- read.csv(
##     "formattedByIndividual_CaliAllYears3_shortFormatted.csv.gz"
## )
## "short" is fewer variables to save memory
names(INDIV)[grep("locality", names(INDIV))] <- c(
    "hid05","hid07","hid09","hid10","hid12"
)

formatYob <- function(x, c1, c2) {
    x <- as.integer(substr(x, c1, c2))
    x[x<1901|x>2000] <- NA
    return(x)
    }

cat("Formatting (1)..\n")
INDIV$yob05 <- formatYob(INDIV$dateofbirth_cali05, 1, 4)
INDIV$yob07 <- formatYob(INDIV$dateofbirth_cali07, 1, 4)
INDIV$yob09 <- formatYob(INDIV$dateofbirth_cali09, 7, 10)
INDIV$yob10 <- formatYob(INDIV$dateofbirth_cali10, 7, 10)
INDIV$yob12 <- formatYob(INDIV$dateofbirth_cali12, 7, 10)


partySwitchSummary <- function(
    x=list(c("05","07"),c("07","09"),c("09","10"),c("10","12"),c("12","14")),
    extrafilter="",
    mover=FALSE,
    lastvote=list(
        "05"="gg06_cali07",
        "07"="pg08_cali09",
        "09"="gg10_cali10",
        "10"="pg12_cali12",
        "12"="pg14_cali14"
    ),

) {
    return(
    lapply(x, function(x) {
        eval(
            parse(
                text=paste("INDIV %>%
    filter(
        !is.na(yob",x[1],")
        & !is.na(yob",x[2],")
        ",extrafilter,
                    ifelse(
                        voters,
                        paste("& ",lastvote[[x[1]]],"==1 "),
                        ""
                    ),
                    ifelse(
                        mover,
                        paste(
                            "& hid",x[1],"!=\"\" & hid",x[2],"!=\"\"",
                            "& hid",x[1],"!=hid",x[2],
                            sep=""),
                        ""
                    ),"
    ) %>%
        group_by(yob",x[1],") %>%
            summarise(
                majorparty=sum(
                    party_cali",x[1]," %in% c(\"dem\",\"rep\")
                    & party_cali",x[2]," %in% c(\"dem\",\"rep\")
                ),
                changeparty=sum(
                    party_cali",x[1],"!=party_cali",x[2],"
                    & party_cali",x[1]," %in% c(\"dem\",\"rep\")
                    & party_cali",x[2]," %in% c(\"dem\",\"rep\")
                ),
                endindependent=sum(
                    party_cali",x[1]," != \"\"
                    & party_cali",x[2]," != \"\"
                    & party_cali",x[2]," %in% c(\"ds\",\"npp\")
                ),
                toindependent=sum(
                    party_cali",x[1]," != \"\"
                    & party_cali",x[2]," != \"\"
                    & !(party_cali",x[1]," %in% c(\"ds\", \"npp\"))
                    & party_cali",x[2]," %in% c(\"ds\",\"npp\")
                ),
                startindependent=sum(
                    party_cali",x[1]," != \"\"
                    & party_cali",x[2]," != \"\"
                    & party_cali",x[1]," %in% c(\"ds\", \"npp\")
                ),
                fromindependent=sum(
                    party_cali",x[1]," != \"\"
                    & party_cali",x[2]," != \"\"
                    & party_cali",x[1]," %in% c(\"ds\", \"npp\")
                    & !(party_cali",x[2]," %in% c(\"ds\",\"npp\")),
                count=n(),
                movers=sum(hid",x[1],"!=hid",x[2],")
                )
            )", sep = ""
                           )
                )
        )
        }
        )
    )
}

cat("Summarizing..\n")
i <- 0
pb <- txtProgressBar(style=3)
INDIV.partySummary <- partySwitchSummary()
i <- i+1/4; setTxtProgressBar(pb, i)
INDIV.partySummary.movers <- partySwitchSummary(mover=TRUE)
i <- i+1/4; setTxtProgressBar(pb, i)
INDIV.partySummary.voters <- partySwitchSummary(voter=TRUE)
i <- i+1/4; setTxtProgressBar(pb, i)
INDIV.partySummary.movers.voters <- partySwitchSummary(mover=TRUE,voter=TRUE)
i <- i+1/4; setTxtProgressBar(pb, i)
close(pb)

cat("Writing..\n")
save(
    INDIV.partySummary,
    INDIV.partySummary.movers,
    INDIV.partySummary.voters,
    INDIV.partySummary.movers.voters,
    file="individual_partychanges_summary.RData"
)

cat("\nDone (EOF).\n")
